package com.ss.project.xia17user.common.redis;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * redis 配置
 * @author xia17
 * @date 2020/5/7
 */
@Configuration
public class RedisConfig {


    /**
     * redisTemplate<String,Object> 实例
     * 使用jackson 序列化
     * @param redisConnectionFactory redis 链接工程
     * @return redis 模板
     */
    @Bean
    public RedisTemplate<String,Object> redisStrTemplate(RedisConnectionFactory redisConnectionFactory,Jackson2JsonRedisSerializer<Object> jacksonSerial){
        //新建一个RedisTemplate
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        //配置连接工厂
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        //设置Value类型的 key-value 序列化方式
        redisTemplate.setValueSerializer(jacksonSerial);
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        // 设置hash key 和value序列化模式
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(jacksonSerial);
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }


    /**
     * redisTemplate<Object,Object> 实例
     * 使用jackson 序列化
     * @param redisConnectionFactory redis 链接工厂
     * @return redis 模板
     */
    @Bean
    public RedisTemplate<Object,Object> redisObjTemplate(RedisConnectionFactory redisConnectionFactory,Jackson2JsonRedisSerializer<Object> jacksonSerial){
        //新建一个RedisTemplate
        RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
        //配置连接工厂
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        //设置Value类型的 key-value 序列化方式
        redisTemplate.setValueSerializer(jacksonSerial);
        redisTemplate.setKeySerializer(jacksonSerial);
        // 设置hash key 和value序列化模式
        redisTemplate.setHashKeySerializer(jacksonSerial);
        redisTemplate.setHashValueSerializer(jacksonSerial);
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }


    /**
     * jackson Redis 序列化实例
     * @return Jackson2JsonRedisSerializer
     */
    @Bean
    public Jackson2JsonRedisSerializer<Object> jacksonSerial(){
        //使用jackson来序列化redis的value值  （使用jdk的方式）
        Jackson2JsonRedisSerializer<Object> jacksonSerial = new Jackson2JsonRedisSerializer<>(Object.class);
        //objectMapper指定怎么序列化
        ObjectMapper objectMapper = new ObjectMapper();
        // 解决无法序列化复杂对象
        objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY,true);
        //指定要序列化的域，field,get和set,以及修饰符范围，ANY是都有包括private和public
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        // 这一句必须要，作用是序列化时将对象全类名一起保存下来,如果没有这个反序列化时对象类型都会丢失，成为HashMap
        objectMapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance,ObjectMapper.DefaultTyping.NON_FINAL);
        //将怎么序列化给json序列化
        jacksonSerial.setObjectMapper(objectMapper);
        return jacksonSerial;
    }


}